package com.hyts.bmsp.tool;

import com.hyts.bmsp.jfinal.annotations.WebMapping;
import com.hyts.bmsp.jfinal.annotations.WebRoute;
import com.hyts.bmsp.jfinal.processor.JfinalClassProcessor;
import com.hyts.bmsp.jfinal.processor.classAttribute;
import com.hyts.bmsp.jfinal.scanner01.JfinalClassScanner;
import org.apache.commons.lang.ClassUtils;
import org.apache.commons.lang.StringUtils;

import java.io.IOException;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * project-name:platform-bmsp
 * package-name:com.hyts.bmsp.tool
 * author:Libo/Alex
 * create-date:2017-12-02 15:37
 * copyright:libo-hyts-github
 * email:libo2dev@aliyun.com
 * description:此类主要用于: Jar包获取classloader操作加载器读取class类文件服务
 */
public final class JarClassLoader {


        private static final  String DEFAULT_FILE_TOKEN_01 = "file:/";

        private static final  String DEFAULT_FILE_TOKEN_02 = "file:\\";


    /**
         * author:Alex/Libo
         * create-date:2017/12/2
         * create-time:22:23
         * method-name:a
         * param:a
         * return:a
         * descripion:a
         */
       public static void loadClass(String jarFile) throws IOException, ClassNotFoundException {

                String jarInfo  = "",jarPackage = "";
                //针对于packagePath 和 base path做出校验
                JarFile jar;
                if(jarFile.indexOf("!") > 0 && jarFile.indexOf("jar")>0)
                {
                        String [] jarSeperate = jarFile.split("!");
                        jarInfo = jarSeperate[0];
                        jarPackage = jarSeperate[1];
                }
                //校验是否进行文件头
                if(jarInfo.startsWith(DEFAULT_FILE_TOKEN_01))
                {
                    jarInfo = jarInfo.replace(DEFAULT_FILE_TOKEN_01,"");
                }
                else{
                    jarInfo = jarInfo.replace(DEFAULT_FILE_TOKEN_02,"");
                }
                JarFile jarObject = new JarFile(jarInfo);
                Enumeration<JarEntry> classList = jarObject.entries();
                    while (classList.hasMoreElements()) {
                        JarEntry entry = classList.nextElement();
                        if (entry.getName().endsWith(classAttribute.COMPILE.getValue())) {
                            Class<?> clazz = ClassUtils.getClass(JfinalClassScanner.getClassPackage(StringUtils.replace(StringUtils.replace(entry.getName(), JfinalClassScanner.PACKAGE_PREFIX + JfinalClassScanner.PACKAGE_SUBFIX, ""), classAttribute.COMPILE.getValue(), "")));
                            if (clazz.getAnnotation(WebMapping.class) != null) {
                                JfinalClassProcessor.processMapping(clazz);
                            } else if (clazz.getAnnotation(WebRoute.class) != null) {
                                JfinalClassProcessor.processRoute(clazz);
                            } else if (clazz.isEnum()) {
                                continue;
                            } else if (clazz.isAnnotation()) {
                                continue;
                            } else if (clazz.isInterface()) {
                                continue;
                            }
                        }
                }
                return ;
       }
}
